
package com.hlkj.pay.service.user.impl;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.util.List;
import java.util.Set;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hlkj.framework.common.pojo.PageParam;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.hlkj.pay.enums.UserEnum;
import com.hlkj.pay.infrastructure.mapper.user.UserCouponInfoMapper;
import com.hlkj.pay.infrastructure.mapper.user.UserCouponMapper;
import com.hlkj.pay.infrastructure.model.user.UserCouponDO;
import com.hlkj.pay.infrastructure.model.user.UserCouponInfoDO;
import com.hlkj.pay.service.user.IUserCouponService;

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;

/**
 * @author HlpayTeam
 * @date 2024/10/25 16:48
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class UserCouponServiceImpl implements IUserCouponService {

    private final UserCouponMapper userCouponMapper;

    private final UserCouponInfoMapper userCouponInfoMapper;

    @Override
    public PageResult<UserCouponDO> userCouponList(Long userId, Integer status, PageParam pageParam) {
        UserCouponDO userCouponDO = new UserCouponDO();
        userCouponDO.setUserId(userId);
        userCouponDO.setStatus(status);
        return userCouponMapper.selectPage(pageParam, buildWrapper(userCouponDO));
    }

    @Override
    public List<UserCouponInfoDO> couponInfoList(Set<Long> couponInfoId) {
        return userCouponInfoMapper.selectBatchIds(couponInfoId);
    }

    @Override
    public void addUserCouponList(List<UserCouponDO> userCouponDOList) {
        userCouponMapper.insertBatch(userCouponDOList);
    }

    @Override
    public UserCouponDO queryUserCoupon(Long id) {
        return userCouponMapper.selectById(id);
    }

    @Override
    public void updateCouponStatus(Long id, UserEnum.USER_COUPON_STATUS userCouponStatus) {
        UserCouponDO userCouponDO = new UserCouponDO();
        userCouponDO.setId(id);
        userCouponDO.setStatus(userCouponStatus.getCode());
        userCouponMapper.updateById(userCouponDO);
    }

    /**
     * 查询条件封装
     *
     * @param userCouponDO
     * @return
     */
    LambdaQueryWrapper<UserCouponDO> buildWrapper(UserCouponDO userCouponDO) {
        LambdaQueryWrapperX<UserCouponDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eqIfPresent(UserCouponDO::getUserId, userCouponDO.getUserId());
        queryWrapper.eqIfPresent(UserCouponDO::getCouponId, userCouponDO.getCouponId());
        queryWrapper.eqIfPresent(UserCouponDO::getStatus, userCouponDO.getStatus());
        return queryWrapper;
    }
}
